<?php
namespace Frieza\Modules\IncreaseFansCard\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\service\IncreaseFansCard as sIncreaseFansCard ;
use Doraemon\model\IncreaseFansCard as mIncreaseFansCard;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Form;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;
use Doraemon\model\Member as mMember;
use Encore\Admin\Grid\Displayers\Actions;
use Doraemon\cache\OnBoardingFansIncrease;
use Doraemon\pockets\constInterface\UserGrowth\OnBoardingTag;


class IncreaseFansCardController extends Controller
{
    const PAGE_SIZE = 20;

    public function __construct()
    {
        parent::__construct();
        Admin::script('$(".pjax-container").attr("id","pjax-container");');
    }

      public function index(Request $request, Content $content)
    {
        $pageSize = $request->input("per_page", self::PAGE_SIZE);
        $page = $request->input("page", 1);
        $search = $request->all();
        $list = sIncreaseFansCard::getList($page, $pageSize, $search);

        $gridModel = new pGridDataModel($list['list'], $list['total'], [
            'perPage' => $pageSize
        ]);

        $gridList = Admin::grid($gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableCreateButton();
                $grid->disableExport();

                $grid->filter(function ($filter) {
                    $filter->disableIdFilter();

                    $filter->column(1/2, function ($filter){
                        $filter->equal('uid', 'uid')->placeholder("请输入用户uid");
//                        $filter->equal('level', '等级')->select(mIncreaseFansCard::LEVEL_RELATION);
                        $filter->equal('status', '状态')->select(mIncreaseFansCard::STATUS_RELATION);
                    });

                    $filter->column(12, function ($filter) {
                        $filter->between("usetime", "涨粉卡排期时间")->datetime();
                    });

                    $filter->expand();
                });
                $grid->column('id');
                $grid->column('uid', 'uid');

                $grid->column("nick", "昵称")->display(function(){
                    if(empty($this->nick)){
                        $userInfo = mMember::getUser($this->uid);
                        return $userInfo["nick"];
                    }else{
                        return $this->nick;
                    }
                });
                $grid->column('status','状态')->using(mIncreaseFansCard::STATUS_RELATION);
                $grid->column('level', '领取时等级')->using(mIncreaseFansCard::LEVEL_RELATION);
                $grid->column('yearmonth', '月份');
                $grid->column('gettime', '领取时间')->display(function () {
                    if ($this->gettime) {
                    return date("Y-m-d H:i:s", $this->gettime);
                    } else {
                        return '--';
                    }
                });
                $grid->column('usetime', '使用时间')->display(function () {
                    if ($this->usetime) {
                        return date("Y-m-d H:i:s", $this->usetime);
                    } else {
                        return '--';
                    }

                });
                $grid->column('exptime', '失效时间')->display(function () {
                    if ($this->exptime) {
                        return date("Y-m-d H:i:s", $this->exptime);
                    } else {
                        return '--';
                    }
                });
                $grid->column("admin_id", '操作人员')->display(function () {
                    return "<span style='width:80px;display: inline-block;'>".Admin::user()->find($this->admin_id)->name."</span>";
                });


                $grid->actions(function ($actions) {
                    $actions->disableDelete();
                    $actions->disableEdit();
                    $actions->disableView();

                    $actions->prepend('<a class="fa reload-data fa fa-ban received-package" data-uid="'.$actions->row->uid.'"  data-id="'.$actions->row->id.'" data-opt = "停止" href="javascript:;" >停止</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
                });
                Admin::script($this->optionSubmit());
            });
        $tab = new Tab();
        $tab->add("停止申请", $gridList->render());


        return $content
            ->header('涨粉卡')
            ->description(' ')
            ->breadcrumb(['text' => '涨粉卡'])
            ->row($gridList);
    }

    //cancellationUser 确认停止逻辑
    public function stop(Request $request)
    {
        $data = [
            'code' => 0,
            'msg' => "停止失败"
        ];
        $iId = $request['id'];
        $iUid = $request['uid'];

        if(!empty($iId) && !empty($iUid)){
            $aUpdate['status'] = mIncreaseFansCard::STATUS_EXP;
            $aUpdate['exptime'] = time();
            $aUpdate['admin_id'] = Admin::user()->id;

            $res = mIncreaseFansCard::updateInfo($aUpdate,['id'=>$request['id']]);
            //删除池子中用户
            OnBoardingFansIncrease::rmOnBoardingFans(OnBoardingTag::INCREASE_FANS, $iUid);
            if($res){
                $data = [
                    'code' => 1,
                    'msg' => "停止成功"
                ];
            }
        }
        return $data;
    }

    //所选操作提交
    public function optionSubmit()
    {
        return <<<SCRIPT
           
            $('.received-package').unbind('click').click(function() {
            var id = $(this).data('id');
            var uid = $(this).data('uid');
            var opt = $(this).data('opt');
            swal({
                title: "是否确认"+opt+"?",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确认",
                showLoaderOnConfirm: true,
                cancelButtonText: "取消",
                preConfirm: function() {
                    if(opt == "停止"){
                        $.post('index/stop', {'id':id,'uid':uid}, function (data) {
                             $.pjax.reload('#pjax-container');
                             if(data.code == 1) {
                                swal(data.msg, '', 'success');
                             } else {
                                swal(data.msg, '', 'error');
                             }                                                                                                                                    
                        });
                    } else {
                        
                                swal(data.msg, '', 'success');
                           
                    }
                    
                }
            });
        });
SCRIPT;
    }

}